# -*- coding:utf-8 -*
from django.shortcuts import render
from helps.utility import get_object_or_none
from django.shortcuts import render_to_response
from django.http import HttpResponse, HttpResponseRedirect
from django.views.decorators.csrf import csrf_exempt
from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger
from accounts.models import User
from accounts.views import *
from jobs.models import *



# Create your views here.
#job管理
def job_admin(request):
    user = request.session.get('user',None)
    if not user:
        return render_to_response('devops/login.html')
    jobs_list =  Admin.objects.all() # 获取表内数据
    jobs_plist = Paginator(jobs_list, 10) # 每页多少个
    page = request.GET.get('page')      # 获取page请求如job?page=xxx
    if page and page.isdigit():         # 判断如果page参数是否存在和者page是否为整数
        page = int(request.GET.get('page')) #int字符串转义成数字类似"123"转义123
    else:
        page = 1                        #默认值设置成1，后期将改成返回404页面
    if page >= 10:                      #列表页显示共10页,1-10,2-11类似等等
        job_info_list = jobs_plist.page_range[page-10:page]
    else:
        job_info_list = jobs_plist.page_range[0:10]
    try:
        job_data = jobs_plist.page(page) #打印出这页有多少个数据
    except PageNotAnInteger:                #不为整数返回第一页数据
        job_data = jobs_plist.page(1)
    except EmptyPage:
        job_data = jobs_plist.page(jobs_plist.num_pages)
    return render_to_response('devops/job.html',{'username':user.name,'job_data':job_data,'plist':job_info_list,'page':page})

